﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using GeoRef;
using Storage;
using Storage.EntityModels;

namespace TripLOA.WebService.georef_test_2
{
    public partial class Test : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {   
                GeoTrip t = new GeoTrip(StorageManager.Instance.GetTripById(1052));
                List<Users_VehicleDetails> vehUsers = new List<Users_VehicleDetails>();
                for (int i = 0; i < StorageManager.Instance.GetUserByUserName("alice").Users_VehicleDetails.Count; i++)
                    vehUsers.Add(StorageManager.Instance.GetUserByUserName("alice").Users_VehicleDetails[i]);

                map2_id.Vehicles = vehUsers;

                map2_id.Editable = true;
                map2_id.EditableTrip = t;

                map2_id.EditableTrip = t;
                map2_id.Editable = true;
				
                
                GeoLoc monta = new GeoLoc(43.66, 10.43, "via san Donato", "Casa mia");
                GeoLoc scendi = new GeoLoc(43.59, 10.41, "Pontedera", "Vo a prende un gelato");
                map2_id.SetPassenger(13, monta, 10.0, scendi, 10.0);
                
                /*
                map2_id.Editable = false;
                map2_id.DrawTrip(t);
                */
               
                return;
                
                
                // creo la lista di veicoli da passare al controllo
                List<Users_VehicleDetails> tmpList = new List<Users_VehicleDetails>();
                Users_VehicleDetails v = new Users_VehicleDetails();
                v.VehicleDetails = new VehicleDetails();
                v.VehicleDetails.VehicleClass = VehicleTypes.Car;
                v.VehicleDetails.Model = "Fiat Uno turbo";
                tmpList.Add(v);

                v = new Users_VehicleDetails();
                v.VehicleDetails = new VehicleDetails();
                v.VehicleDetails.VehicleClass = VehicleTypes.Train;
                v.VehicleDetails.Model = "FS";
                tmpList.Add(v);

                v = new Users_VehicleDetails();
                v.VehicleDetails = new VehicleDetails();
                v.VehicleDetails.VehicleClass = VehicleTypes.Bike;
                v.VehicleDetails.Model = "Graziella";
                tmpList.Add(v);

                // assegno la lista di veicoli al controllo
                map2_id.Vehicles = tmpList;

                // centro la mappa su "Pisa"
                GeoQuery q = new GeoQuery();
                //map2_id.MapCenter = "prop";


                // faccio disegnare un GeoTrip
                GeoTrip trip;
                GeoLoc start;
                GeoLoc end;
                List<GeoLoc> l = new List<GeoLoc>();
                GeoQuery query = new GeoQuery();

                start = new GeoLoc("43.676830,10.690780", "San Donato", "Casa mia");
                end = new GeoLoc("43.72055948722688,10.407872200012207", "Università", "Arrivato!");
                l.Add(new GeoLoc("43.681630,10.562980", "prr", ""));
                trip = new GeoTrip(start, end, l);
                GeoLoc nextRouteEnd = query.geoCoding("italy", "rosignano marittimo")[0];
                GeoRoute nextTappa = new GeoRoute(end, nextRouteEnd);
                nextTappa.Vehicle = VehicleType.plane;
                nextTappa.InsertViaPoint(query.geoCoding("italy", "castiglioncello, livorno")[0]);
                nextTappa.InsertViaPoint(query.geoCoding("italy", "santa luce")[0]);
                trip.AddRoute(nextTappa);

                trip.UpdatePath();
                
                map2_id.Editable = true;
                map2_id.EditableTrip = trip;
                
                string jsCode = "this._map._editableTrip._markers[1]._geoloc.gettingInUsers = [10];" +
                                "this._map._editableTrip._markers[1].setLocked(false);";
                map2_id.InvokeJSCommand(jsCode);
                
                /*
                map2_id.DrawTrip(trip);
                string jsCode = "this._map._trips[0].setDeviationRadius(3000);";
                map2_id.InvokeJSCommand(jsCode);
                map2_id.Editable = false;
                */
            }
        }

        protected void ClickButton1(object sender, EventArgs e)
        {
            GeoTrip x = map2_id.EditableTrip;
            GeoLoc arr = map2_id.PassengerArrive;
            GeoLoc dep = map2_id.PassengerDeparture;

            if (x.IsConsistent)
            {   
                Console.WriteLine(dep);
                Console.WriteLine(arr);
                Console.WriteLine(map2_id.PassengerArrive);
                Console.WriteLine(map2_id.PassengerDeparture);
                int i = 0;
                i = x.Count;
                i++;
            }
        }

        protected void Passenger_Click(object sender, EventArgs e)
        {
            GeoLoc monta = new GeoLoc(43.67, 10.68, "via san Donato", "Casa mia");
            GeoLoc scendi = new GeoLoc(43.66, 10.63, "Pontedera", "Vo a prende un gelato");
            //map2_id.SetPassenger(13, monta, 0.5, scendi, 1.0);
            map2_id.SetPassengerDeparture(13, monta, 2.0);
            map2_id.SetPassengerArrive(13, scendi, 5.0);
        }

        protected void InsertingRoute_Btm(object sender, EventArgs e)
        {
            int rInd = int.Parse(routeIndex.Text);
            if (rInd != -1)
            {
                int wInd = int.Parse(waypointIndex.Text);
                GeoLoc newStage = new GeoLoc(coord.Text,"","");
                string jsCode = "this._map._editableTrip.insertStageFromGeoloc(" +
                    GeoSerializer.ToJsonString(typeof(GeoLoc), newStage) +
                    "," + rInd + "," + wInd + ");";
                map2_id.InvokeJSCommand(jsCode);
            }
        }

        protected void RemovingRoute_Btm(object sender, EventArgs e)
        {
            string jsCode = "{var marker2Del = this._map._editableTrip._markers[" +
                    int.Parse(markerStageIndex.Text) + "];" +
                    "if(this._map._editableTrip.removeStageFromMarker(marker2Del) )" +
                    "this._map.removeOverlay(marker2Del);}";
            map2_id.InvokeJSCommand(jsCode);
        }

        protected void changingPassenger(object sender, EventArgs e)
        {
            GeoLoc center = new GeoLoc("43.2,10.6", "", "");
            map2_id.ChangePassengerArriveCenter(center);
            map2_id.ChangePassengerDepartureRadius(13);
        }

        protected void Button7_Click(object sender, EventArgs e)
        {
            /*
            map2_id.ClearMap();
            map2_id.Editable = true;
            map2_id.EditableTrip = new GeoTrip(StorageManager.Instance.GetTripById(1052));
            */ 
        }        
    }
}
